//
//  DLGroupManagerVC.m
//  DontRem
//
//  Created by apple on 2021/1/26.
//

#import "DLGroupManagerVC.h"
#import "DLGroupModel.h"
#import "DLGroupManagerCell.h"
@interface DLGroupManagerVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView * myTab;
@property(nonatomic,retain)NSMutableArray * dataArr;
@end

@implementation DLGroupManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTitle = @"分类管理";
    self.view.backgroundColor = [UIColor normalColor:UnderLineColor darkColor:UIColorBlack];
    self.dataArr = [NSMutableArray new];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadSelf) name:DeleteOrEditGroupNotification object:nil];
    [self getData:NO];
}
-(void)reloadSelf{
    [self.dataArr removeAllObjects];
    [self getData:YES];
}
-(void)getData:(BOOL)isReload{
    NSArray* finfAlls = [DLGroupModel bg_findAll:DLGroupTab];
    [self.dataArr addObjectsFromArray:finfAlls];
    if (isReload) {
        [self.myTab reloadData];
    }else{
        [self createUI];
    }
}
-(void)addGroupClick{
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"温馨提示";
    [dialogViewController addTextFieldWithTitle:@"分类名称" configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"请输入分类名称";
        textField.maximumTextLength = 10;
    }];
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        UITextField * tf = aDialogViewController.textFields.firstObject;
        if (tf.text.length > 0) {
            if ([tf.text isEqualToString:@"默认分组"]) {
                XHQHUDErrorText(@"不能与系统关键词重名，请重新选择");
            }else{
                [aDialogViewController hide];
                DLGroupModel * mo = [DLGroupModel new];
                mo.groupName = tf.text;
                mo.bg_tableName = DLGroupTab;
                [mo bg_save];
                [self.dataArr removeAllObjects];
                [self getData:YES];
            }
        } else {
            XHQHUDErrorText(@"分类名称不能为空");
        }
    }];
    [dialogViewController show];
}
#pragma mark 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DLGroupManagerCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[DLGroupManagerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (indexPath.row < self.dataArr.count) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footer = [UIView new];
    footer.backgroundColor = UnderLineColor;
    CGFloat w = SCREEN_WIDTH - 20;
    QMUIButton * btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = Frame(10, 10, w, 50);
    [btn setTitle:@"添加分组" forState:UIControlStateNormal];
    [btn setTitleColor:TextSubColor forState:UIControlStateNormal];
    btn.titleLabel.font = UIFontMake(floor(AUTO(18)));
    btn.spacingBetweenImageAndTitle = 10;
    [btn setImagePosition:QMUIButtonImagePositionLeft];
    [btn setImage:Image(@"添加_方框") forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addGroupClick) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    CAShapeLayer *layer   = [[CAShapeLayer alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:Frame(0, 0, w, 50) cornerRadius:4.0f];
    layer.path = path.CGPath;
    layer.lineWidth = 1.0f;
    layer.lineDashPattern = @[@4, @4];
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor grayColor].CGColor;
    [btn.layer addSublayer:layer];
    return footer;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(void)createUI{
    self.myTab = [[UITableView alloc]initWithFrame:Frame(0, kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - KTabbarSafeBottomMargin) style:UITableViewStyleGrouped];
    self.myTab.delegate = self;
    self.myTab.dataSource = self;
    self.myTab.backgroundColor = [UIColor normalColor:UnderLineColor darkColor:UIColorBlack];
    self.myTab.rowHeight = 55;
    self.myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTab];
}

@end
